<template>
  <div>
    <div class="video">
      <video :src="videoSrc" controls :poster="cover" ref="video"></video>
      <!--      commentCount-->

      <div class="iocn">
        <!--        评论-->
        <span class="artistName">
          作者：<br>
          {{ artistName }}
        </span>
        <van-icon class="van-icon" @click=toComments(id) name="chat-o" :badge="commentCount" size="30"/>
        <van-icon class="van-icon" name="like-o" :badge="likeCount" size="30"/>
      </div>
    </div>
  </div>
</template>

<script>
import {getMVData, getMVUrl} from "@/api/login-aip";
import {mapState} from "vuex";

export default {
  name: "MVListData",
  data() {
    return {
      mvlistData: '',
      videoSrc: '',
      commentCount: '',
      likeCount: '',
      cover: ''
    }
  },
  props: {
    id: [Number,String],
    artistName: String
  },
  created() {
    // console.log(this.id)
    //  控制只能有一个video播放
    var videos = document.getElementsByTagName('video');
    for (var i = videos.length - 1; i >= 0; i--) {
      (function () {
        var p = i;
        videos[p].addEventListener('play', function () {
          pauseAll(p);
        })
      })()
    }

    function pauseAll(index) {
      for (var j = videos.length - 1; j >= 0; j--) {
        if (j != index) videos[j].pause();
      }
    }

    getMVData(this.id).then(res => {
          this.cover = res.data.cover
          this.commentCount = res.data.commentCount
          this.likeCount = res.data.likeCount
          getMVUrl(this.id).then(res => {
            // console.log(res.data)
            this.videoSrc = res.data.url
          })
        }
    )
  },
  computed: {
    ...mapState(["MVId"])
  },
  methods: {
    toComments(e) {
      this.$store.commit("changeMVid", {MVId: e})
      this.$router.push({
        path:'/MVComments',
        query:{
          MVId:e
        }
      })
    }
  },
  updated() {
    getMVData(this.id).then(res => {
          // console.log(res.data)
          this.cover = res.data.cover
          this.commentCount = res.data.commentCount
          this.likeCount = res.data.likeCount
          getMVUrl(this.id).then(res => {
            // console.log(res.data)
            this.videoSrc = res.data.url
          })
        }
    )
  }
}
</script>

<style scoped lang="less">
.video {
  display: flex;
  flex-direction: column;
  padding: 0 20px;


  .iocn {
    position: relative;
    margin-top: 10px;
    display: flex;
    justify-content: center;

    .artistName {
      display: inline-block;
      width: 100px;
      height: 50px;
      font-size: 16px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: #000;
      position: absolute;
      left: 2px;
      top: 0px;
    }

    .van-icon {
      margin: 10px 30px;
    }
  }
}

video {
  width: 100%;
}
</style>